package com.theme.passport.controller.theme;

import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.entity.theme.WallpaperClassifyEntity;
import com.theme.passport.service.theme.WallpaperClassifyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**
 * 壁纸分类表
 *
 * @author live
 * @email 870459550@qq.com
 * @date 2022-06-21 10:01:49
 */
@RestController
@RequestMapping("wallpaper/wallpaper-classify")
public class WallpaperClassifyController {

    @Autowired
    private WallpaperClassifyService wallpaperClassifyService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("wallpaper:wallpaper-classify:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wallpaperClassifyService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/listTree")
    public R listTree() {
        List<WallpaperClassifyEntity> classifyEntityList = wallpaperClassifyService.list();
        for (WallpaperClassifyEntity wpClassifyEntity : classifyEntityList) {
            WallpaperClassifyEntity parentClassifyEntity = wallpaperClassifyService.getById(wpClassifyEntity.getParentId());
            if (parentClassifyEntity != null) {
                wpClassifyEntity.setParentName(parentClassifyEntity.getName());
            }
        }
        return R.ok().put("data", classifyEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/listParent")
    @RequiresPermissions("theme:theme-classify:list")
    public R listParent(@RequestParam(value = "parentId", required = false) Long parentId,
                        @RequestParam(value = "isParent", required = false) Integer isParent) {
        Map<String, Object> params = new HashMap<>();
        if (parentId != null) {
            params.put("parent_id", parentId);
        }
        if (isParent != null) {
            params.put("is_parent", isParent);
        }
        params.put("page", 1);
        params.put("limit", 100000);
        PageUtils pages = wallpaperClassifyService.queryPage(params);
        return R.ok().put("data", pages.getList());
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("wallpaper:wallpaper-classify:info")
    public R info(@PathVariable("id") Long id){
		WallpaperClassifyEntity WallpaperClassify = wallpaperClassifyService.getById(id);

        return R.ok().put("wallpaperClassify", WallpaperClassify);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("wallpaper:wallpaper-classify:save")
    public R save(@RequestBody WallpaperClassifyEntity wallpaperClassify){
        wallpaperClassify.setCreated(new Date());
        wallpaperClassifyService.save(wallpaperClassify);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("wallpaper:wallpaper-classify:update")
    public R update(@RequestBody WallpaperClassifyEntity wallpaperClassify){
        wallpaperClassify.setUpdated(new Date());
        wallpaperClassifyService.updateById(wallpaperClassify);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("wallpaper:wallpaper-classify:delete")
    public R delete(@RequestBody Long[] ids){
        wallpaperClassifyService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
